package com.jy.adscm.operation.pebase.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeAbstractContent;
import com.jy.adscm.operation.pebase.bean.QueryPeAbstractContent;
import com.jy.adscm.operation.pebase.service.PeAbstractContentService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-04-12
 */
@RestController
@RequestMapping("/adscm/pebase/peAbstractContent")
public class PeAbstractContentController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeAbstractContentService peAbstractContentService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody PeAbstractContent PeAbstractContent, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(PeAbstractContent.getDid(),null)){
            throw new  AdscmSystemException("did不存在");
        }
        PeAbstractContent.setCreator(token.getUser().getId().toString());
        PeAbstractContent.setCreateTime(new Date());
        boolean insert = peAbstractContentService.insert(PeAbstractContent);
        return new SimpleCoreResponse("200","新增成功",PeAbstractContent.getId());
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id , HttpServletRequest request)throws AdscmSystemException {
        boolean result =false;
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        PeAbstractContent PeAbstractContent = peAbstractContentService.selectById(id);
        if (  PeAbstractContent.getCreator().equals(token.getUser().getId().toString())){
            result=  peAbstractContentService.deleteById(id);
        }
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody PeAbstractContent PeAbstractContent, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        PeAbstractContent.setCreator(token.getUser().getId().toString());
        PeAbstractContent.setCreateTime(new Date());
        Boolean result = peAbstractContentService.updateById(PeAbstractContent);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectByDid")
    public SimpleCoreResponse select(@RequestBody QueryPeAbstractContent queryPeAbstractContent, HttpServletRequest request) throws AdscmSystemException{
        Page<PeAbstractContent> page =new Page<>(queryPeAbstractContent.getCurrentPage(),queryPeAbstractContent.getPageSize());
        page= peAbstractContentService.selectPeAbstractContent(page,queryPeAbstractContent);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
}

